﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace AGuang.WebTool {
    public static class StringFilter {
        /// <summary>
        /// 为Javascript提供字符串
        /// </summary>
        public static String StringForJavascript(String str) {
            StringBuilder sb = new StringBuilder(str);
            sb.Replace("\\", "\\\\");
            sb.Replace("\n", "\\n");
            sb.Replace("\r", "\\r");
            sb.Replace("\t", "\\t");
            sb.Replace("\"", "\\\"");
            return "\"" + sb.ToString() + "\"";
        }

        /// <summary>
        /// 将字符串转换为HTMl格式
        /// </summary>
        public static String String2Html(String str) {
            StringBuilder sb = new StringBuilder(str);
            sb.Replace("&", "&amp;");
            sb.Replace("  ", " &nbsp;");
            sb.Replace("<", "&lt;");
            sb.Replace(">", "&gt;");
            sb.Replace("\t", "&#9;");
            sb.Replace("\r\n", "<br />");
            sb.Replace("\n", "<br />");
            sb.Replace("\r", "<br />");
            return sb.ToString();
        }

        /// <summary>
        /// 清除html
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static String ClearHTML(String html) {
            Regex regex = new Regex("<.+?>");
            return regex.Replace(html, "");
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="text"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static String TextCut(String text, int len) {
            if (text.Length < len) return text;
            else {
                String t = text.Substring(0, len) + "...";
                return t;
            }
        }

        /// <summary>
        /// 为Sql提供字符串
        /// </summary>
        public static String StringForSql(String str) {
            StringBuilder sb = new StringBuilder(str);
            sb.Replace("'", "''");
            return "'" + sb.ToString() + "'";
        }

        /// <summary>
        /// 是否为电子邮件
        /// </summary>
        public static bool IsEmail(String str) {
            return TestString(str, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        }

        /// <summary>
        /// 是否与正则匹配
        /// </summary>
        public static bool TestString(String str, String pattern) {
            Regex regex = new Regex(pattern);
            MatchCollection matchs = regex.Matches(str);
            if (matchs == null || matchs.Count < 1) return false;
            if (matchs[0].Value == str) return true;
            return false;
        }
        /// <summary>
        /// 是否为电话号码
        /// </summary>
        public static bool IsTelephone(String str) {
            return TestString(str, @"(\(0\d{2,3}\)|0\d{2,3}-?)?\d{7,8}");
        }

        /// <summary>
        /// 是否为手机号码
        /// </summary>
        public static bool IsMobile(String str) {
            return TestString(str, @"((\+|0)?86)?1[35689]\d{9}");
        }

        /// <summary>
        /// 是否为数字
        /// </summary>
        public static bool IsNumber(String str) {
            return TestString(str, @"\d+\.?\d*");
        }

        /// <summary>
        /// 是否为整数
        /// </summary>
        public static bool IsInt(String str) {
            return TestString(str, @"(\(0\d{2,3}\)|0\d{2,3}-?)?\d{7,8}");
        }

        /// <summary>
        /// 是否全为中文
        /// </summary>
        public static bool IsChinese(String str) {
            return TestString(str, @"[\u4e00-\u9fa5]+");
        }

        /// 转全角的函数(SBC case)
        ///
        ///任意字符串
        ///全角字符串
        ///
        ///全角空格为12288，半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        ///

        /// <summary>
        /// 转成全角的函数(SBC case)
        /// </summary>
        public static String ToSBC(String input) {
            // 半角转全角：
            if (String.IsNullOrEmpty(input)) return input;
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++) {
                if (c[i] == 32) {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new String(c);
        }

        /**/
        // /
        // / 转半角的函数(DBC case)
        // /
        // /任意字符串
        // /半角字符串
        // /
        // /全角空格为12288，半角空格为32
        // /其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        /// <summary>
        /// 转成半角的函数(SBC case)
        /// </summary>
        public static String ToDBC(String input) {
            if (String.IsNullOrEmpty(input)) return input;
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++) {
                if (c[i] == 12288) {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new String(c);
        }


    }
}